<template>
  <div class="prize">
    <div class="prize-back">
      <div class="prize-back-left" @click="back">&lt;</div>
      <div class="prize-back-right">我的奖品</div>
      <div></div>
    </div>
    <ul class="prize-content">
      <li
        class="prize-content-list"
        v-for="(item, index) of drawList"
        :key="index"
      >
        <div class="prize-content-list-left">
          <img class="prize-content-list-left-img" :src="item.pic" alt="" />
          <div class="prize-content-list-left-text">
            <p class="prize-content-list-left-text-title">{{ item.name }}</p>
            <p class="prize-content-list-left-text-content" v-if="item.phone!=null && item.phone!=''">已兑换至 {{  item.phone }} 账号</p>
            <p class="prize-content-list-left-text-content">
              {{ item.createTime }}
            </p>
          </div>
        </div>
        <div
          v-if="item.isExchange == '开始兑换'"
          class="prize-content-list-right"
          @click="exShow(item)"
        >
          {{ item.isExchange }}
        </div>
        <div
          v-else
          class="prize-content-list-right"
          style="background: rgba(227, 74, 76, 0.5)"
        >
          {{ item.isExchange }}
        </div>
      </li>
    </ul>
    <van-popup v-model="excShow" round :style="{ width: '70%' }">
      <div class="prize-coupon">
        <div class="prize-coupon-content">
          <div>
            <van-field v-model="phoneValue" maxlength="11" placeholder="请输入您的手机号！" />
          </div>
        </div>
        <div class="prize-coupon-bottom" @click="exchange">立即兑换</div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { historyList, exchangePrize } from "../api/LuckyDraw";
export default {
  data() {
    return {
      drawList: {}, //奖品列表
      appid: "",
      phoneValue:"",//手机号
      excShow:false,//券的弹窗
      itemid:'',//中奖类型
      id:"",
    };
  },
  mounted() {
    this.appid = this.$route.query.appId;
    this.drawHistory();
  },
  methods: {
    // 返回
    back() {
      window.history.back();
    },
    // 开始兑换
    exShow(item) {
      this.itemid = item.itemId
      this.id = item.id
      if (item.itemId !== "2") {
        this.excShow = true;
      } else {
        this.duihuan({
          appId: this.appid,
          itemId: item.itemId,
          id: item.id,
        });
      }
    },

    // 立即兑换
    exchange() {
      let phonetast =
      /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
          this.phoneValue
        );
      if (!phonetast) {
        this.$toast.fail("您的手机号输入有误！");
        return;
      }
      this.duihuan({
        appId: this.appid,
        itemId: this.itemid,
        id: this.id,
        phone:this.phoneValue
      });
    },
    // 兑换接口
    duihuan(data) {
      exchangePrize(data)
        .then((res) => {
          if (res.data.code === 200) {
            this.drawList.map((item) =>{
              if(item.id === res.data.data.id) {
                item.phone = res.data.data.phone
              }
            })
            this.excShow = false
            this.$toast.success("兑换成功");
            this.drawHistory();
          } else {
            this.$toast.fail(res.data.msg);
          }
        })
        .catch(() => {
          this.$toast.fail("服务器开小差了，请稍后重试");
        });
    },
    // 查询奖品列表
    async drawHistory() {
      await historyList({ appId: this.appid })
        .then((res) => {
          if (res.data.code === 200) {
            let Awarded = res.data.rows.filter((item) => {
              return item.itemId != 5;
            });
            Awarded.map((item) => {
              if (item.isExchange == "1") {
                item.isExchange = "已兑换";
              } else if (item.isExchange == "2") {
                item.isExchange = "已过期";
              } else if (item.isExchange == "0") {
                item.isExchange = "开始兑换";
              }
            });
            this.drawList = Awarded;
            console.log("this.drawList",this.drawList)
          } else {
            this.$toast.fail(res.data.msg);
          }
        })
        .catch(() => {
          this.$toast.fail("服务器开小差了，请稍后重试");
        });
    },
  },
};
</script>

<style lang="scss" scoped>
img {
  width: 100%;
  height: 100%;
}
.prize {
  background: url(../assets/yuedui.webp);
  background-size: 100% 100%;
  &-back {
    color: white;
    padding: 0.5rem;
    display: flex;
    justify-content: space-between;
    font-size: 0.5rem;
  }
  &-content {
    background: white;
    border-radius: 0.5rem 0.5rem 0 0;
    overflow: hidden;
    &-list {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: rgba(247, 195, 213, 0.5);
      border-radius: 0.2rem;
      margin: 0.6rem;
      padding: 0.3rem;
      &-left {
        display: flex;
        align-items: center;
        text-align: left;
        &-text {
          &-title {
            color: #e23353;
          }
          &-content {
            color: #898787;
          }
        }
        &-img {
          width: 1rem;
          margin-right: 0.6rem;
        }
        p {
          line-height: 0.5rem;
        }
      }
      &-right {
        background: #e4594a;
        color: white;
        padding: 0.2rem;
        border-radius: 0.1rem;
        width: 2rem;
      }
    }
  }
  &-coupon {
    background: #ca352c;
    padding: 0.3rem;
    display: flex;
    flex-direction: column;
    &-header {
      display: flex;
      align-items: center;
      // flex: 0 0 1.5rem;
      justify-content: center;
      &-content {
        color: white;
        font-size: 0.5rem;
      }
    }
    &-content {
      // flex: 0 0 2rem;
      background: white;
      border-radius: 0.3rem;
      background-size: 10%;
      margin: 0.5rem 0;
      position: relative;
      &-jiangxiang {
        width: 90%;
      }
      &-quan {
        position: absolute;
        bottom: -0.5rem;
        width: 100%;
      }
      &-text {
        position: absolute;
        top: 50%;
        bottom: 0;
        color: white;
        left: 0;
        right: 0;
      }
    }

    &-bottom {
      flex: 0 0 1rem;
      background: #f9d574;
      line-height: 1rem;
      border-radius: 0.2rem;
    }
  }
}
.van-cell {
  border-radius: 1rem;
}
</style>